default_target: all

NAME = balzebub
CC = g++

# For libgcrypt support (delete if you don't use bzbcrypto)
GNUTLSFLAGS = `pkg-config gnutls --cflags --libs`
GCRYPTFLAGS = -I/usr/local/include -L/usr/local/lib -lgcrypt -lgpg-error -DBZBCRYPTO_UNSTABLE

CXXFLAGS = -Wall -std=c++11 -I./include/ -pedantic $(GCRYPTFLAGS) $(GNUTLSFLAGS)
CXXFLAGS32 = -Wall -std=c++11 -I./include/ -pedantic -m32 # $(GCRYPTFLAGS) $(GNUTLSFLAGS)

SOURCES = $(wildcard src/*.cpp)
OBJECTS = $(patsubst src/%.cpp, %.o, $(SOURCES))

# Colored output constants
GREEN = -e "\033[0;92m"
RESET = "\033[0;0m"

TESTABLE_MODULES = bzbstring bzbcrypto bzbfile
TOOLS = balzebub enumhost execshellcode elfinfo elfswitch tcpclient

run_tests:
	@echo + Running all test cases...
	@for t in ${TESTABLE_MODULES} ; do \
		echo Running test: $$t ; \
		./test/test-$$t ; \
	done
	@echo Finished testing

compile-test-bzbstring:
	@echo + Compiling bzbstring test...
	@${CC} test/test-bzbstring.cpp -std=c++11 -I./include/ -DCATCH_FAST_COMPILE -o test/test-bzbstring

compile-test-bzbcrypto:
	@echo + Compiling bzbcrypto test...
	@${CC} test/test-bzbcrypto.cpp src/bzbutil.cpp src/bzbcrypto.cpp -std=c++11 -I./include/ $(GCRYPTFLAGS) -DCATCH_FAST_COMPILE -o test/test-bzbcrypto

compile-test-bzbfile:
	@echo + Compiling bzbfile test...
	@${CC} test/test-bzbfile.cpp src/bzbutil.cpp src/bzbfile.cpp -std=c++11 -I./include/ $(GCRYPTFLAGS) -DCATCH_FAST_COMPILE -o test/test-bzbfile

tests: compile-test-bzbstring compile-test-bzbcrypto compile-test-bzbfile
	@echo ${GREEN}Finished compiling tests${RESET}

shared:
	@echo + Compiling shared object...
	@${CC} -shared ${SOURCES} ${CXXFLAGS} -fPIC -o bin/libbzb.so
	@echo ${GREEN}OK${RESET}

example:
	@echo + Compiling example program...
	@${CC} ${SOURCES} test/example.cpp ${CXXFLAGS} -o test/example
	@echo ${GREEN}OK${RESET}

example32:
	@echo + Compiling 32-bit example program...
	@${CC} ${SOURCES} test/example.cpp ${CXXFLAGS32} -o test/example32
	@echo ${GREEN}OK${RESET}

tools:
	@echo + Compiling tool programs...

	@echo + Compiling balzebub...
	@${CC} tool/balzebub.cpp src/bzbutil.cpp src/bzbfile.cpp -std=c++11 -I./include/ -o bin/balzebub

	@echo + Compiling elfinfo...
	@${CC} tool/elfinfo.cpp src/bzbutil.cpp src/bzbfile.cpp -std=c++11 -I./include/ -o bin/elfinfo

	@echo + Compiling elfswitch...
	@${CC} tool/elfswitch.cpp src/bzbutil.cpp src/bzbfile.cpp -std=c++11 -I./include/ -o bin/elfswitch

	@echo + Compiling enumhost...
	@${CC} tool/enumhost.cpp src/bzbutil.cpp src/bzbnet.cpp -std=c++11 -I./include/ -o bin/enumhost

	@echo + Compiling execshellcode...
	@${CC} tool/execshellcode.cpp -std=c++11 -I./include/ -o bin/execshellcode

	@echo + Compiling tcpclient...
	@${CC} tool/tcpclient.cpp src/bzbutil.cpp src/bzbnet.cpp -std=c++11 -I./include/ -o bin/tcpclient

	@echo ${GREEN}Finished compiling tool programs${RESET}

install:
	@echo + Installing libbzb...
	@sudo cp ./bin/libbzb.so /usr/local/lib/libbzb.so
	@echo ${GREEN}Installed in /usr/local/lib/${RESET}

	@echo + Installing tools...
	@for t in ${TOOLS} ; do \
		echo Installing: $$t ; \
		sudo cp ./bin/$$t /usr/local/bin/$$t ; \
	done
	@echo ${GREEN}Installed in /usr/local/bin/${RESET}

	@echo + Copying header files...
	@if [[ -d /usr/local/include/bzb/ ]] ; then \
		sudo rm -rf /usr/local/include/bzb/ ; \
	fi
	@sudo cp -r ./include/ /usr/local/include/bzb/
	@echo ${GREEN}Copied to /usr/local/include/bzb/${RESET}

all: shared example tests run_tests tools

help:
	@echo "The following are some of the valid targets for this Makefile:"
	@echo "... all (the default if no target is provided)"
	@echo "... shared (compile the shared library)"
	@echo "... example (compile the test program)"
	@echo "... example32 (compile the test program for 32-bit systems)"
	@echo "... tests (compile all test programs)"
	@echo "... run_tests (run all test programs)"
	@echo "... tools (compile all tools)"
	@echo "... clean"
